#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fileencoding=utf-8
# vim:tabstop=4
#
# copyright (c) 2010 by The Codedrawer Team
#
# 成员
# xkhhdx(HD.Huo)	viking9345(C.Cai)	renxing225(X.Ren)
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# 代码来自：http://code.google.com/p/codedrawer/
#
#--- 更新记录 ---
#成员        时间        	简述
#xkhhdx		20:30 2010-8-29	添加了对行号的支持
#xkhhdx		16:37 2010-7-21	添加了 HTML 的转换风格控制模块
#
#--- 计划列表 ---
#提出者        时间        	是否解决	解决者	计划简述        解决简述
#

import re

class style_html():
    def prepare( self ):
        self.parent.marker_dict["comment"] = r"<span style='color: rgb(0, 136, 0); font-style: italic; '>\g<0></span>"
        self.parent.marker_dict["quoted_string"] = r"<span style='color: rgb(0, 0, 255); '>\g<0></span>"
        self.parent.marker_dict["keyword"] = r"<span style='color: rgb(0, 0, 128); font-weight: bold;'>\g<0></span>"

    def prerender_escape_sequence( self, text, amp_rep = False ):
        """
        src:http://114.xixik.com/character/
        HTML特殊转义字符列表
        	最常用的字符实体
        Character Entities
        显示 	说明 	实体名称 	实体编号
        	半方大的空白 	&ensp; 	&#8194;
        	全方大的空白 	&emsp; 	&#8195;
        	不断行的空白格 	&nbsp; 	&#160;
        < 	小于 	&lt; 	&#60;
        > 	大于 	&gt; 	&#62;
        & 	&符号 	&amp; 	&#38;
        " 	双引号 	&quot; 	&#34;
        © 	版权 	&copy; 	&#169;
        ® 	已注册商标 	&reg; 	&#174;
        ™ 	商标（美国） 	™ 	&#8482;
        × 	乘号 	&times; 	&#215;
        ÷ 	除号 	&divide; 	&#247;
        """

        if not amp_rep:
            text = text.replace( "<", "&lt;" )
            text = text.replace( ">", "&gt;" )

        if amp_rep:
            text = text.replace( "&", "&amp;" )

        text = text.replace( "\"", "&quot;" )
        text = text.replace( " ", "&nbsp;" )
        text = text.replace( "\t", "&nbsp;" * 4 )

        return text

    def postrender_escape_sequence( self, text ):
        """  """

        return text

    def rearrange( self, text, showlinenumber = True ):
        """  """

        if not showlinenumber:
            return text.replace( "\n", " <br/>\n" )

        lines = text.replace( "\n", " <br/>\n" ).split("<br/>"); text = ""

        magnitude = len("%d" % ( len(lines) ) )

        for i in range(0, len(lines)):
        	text = text + "<br/>" + self.make_line_number(i+1, magnitude) + lines[i]
        return text

    def make_line_number( self, num, magnitude ):
        return '<span class="line_num" style = "%s text-align:right;">' % ( num % 5 and [ "color: rgb(0, 136, 0); font-style: italic;" ] or ["color: rgb(248, 16, 176);"])[0] + self.prerender_escape_sequence(('%%0%dd'%magnitude) % num) + '&nbsp;</span>'

if __name__ == '__main__':
    print u"如果要测试这个模块 请使用 ./test.py"

